<? $frmtime = time(); ?>
<div>
    <form method="POST" id="frmFilter<?=$frmtime?>">
        <div class="filterdiv" id="fd<?=$frmtime?>">
            <h1 class="fiter-title">Filter your search</h1>
            <?   
                foreach($items as $fitem) {
            ?>
                <div class="fitem">
                <? 
                    $this->load->view('elements/filteritem',$fitem);        
                ?> 
                </div>
            <?
                }
            ?>
                <div class="fitem">
                    <label>Show All</label> 
                    <input type="checkbox" name="show_all" value="1" <? if($show_all) echo "CHECKED=\"true\""; ?> />
                </div>
            <div class="clr"></div> 
            <?
            if($meta) {
                $col_selected = false;
                if( is_array($cols) && count($cols) > 0) {
                    $col_selected = true;
                }
            ?>
            <div class="coldiv" id="fd<?=$frmtime?>">
                <h1 class="fiter-title">Select Report Columns. 
                <?
                    if(!$col_selected) {
                ?>
                        <a id="cda<?=$frmtime?>" href="javascript:void(0)">Show</a>
                <?
                    } else {
                ?>
                        <a id="cda<?=$frmtime?>" href="javascript:void(0)">Hide</a>
                <?        
                    }
                ?>
                </h1>
                <div class="divcolsel" id="cds<?=$frmtime?>">
                    <? 
                    $fields = $meta["fields"];
                    
                    if($fields) {
                    ?>
                    <select id="columns<?=$frmtime?>" class="multiselect" multiple="multiple" name="columns[]">
                        <?
                            if($col_selected) {
                                foreach($cols as $col) {
                                    $selected_col = $fields[$col];
                            ?>
                                <option value="<?=$col?>" selected="selected"><?=$selected_col["label"]?></option>
                            <?
                                }
                            
                                foreach($fields as $key => $field) {
                                    if(!in_array($key,$cols)) {
                            ?>
                                        <option value="<?=$key?>"><?=$field["label"]?></option>
                            <?
                                    } else {
                                        
                                    }
                                }
                            } else {
                                foreach($fields as $key => $field) {
                            ?>
                                    <option value="<?=$key?>"><?=$field["label"]?></option>
                            <?
                                }
                            }
                        ?>
                    </select>
                    
                    <script type="text/javascript">
                    $(document).ready( function() {
                      $("#columns<?=$frmtime?>").multiselect({searchable: false});
                    });
                    </script>
                    <?
                    }
                    ?>
                </div>
            </div>
            <?
            }
            ?>
            <div class="search-btn-div">
                <input class="button" type="submit" name="<?=$submit?>" value="Search" />
                <input class="button" type="reset" name="reset" value="Reset" />
            </div>       
        </div>    
        
    </form>
    <script type="text/javascript" language="javascript">
        if(typeof jQuery == 'undefined') {
            alert('Invalid Request');
            window.location = "/";
        }
        $(document).ready( function() {
            $("#frmFilter<?=$frmtime?>").validate({
                rules: {
                    mgposition: {
                        number: true
                    }        
                },
                errorPlacement: function(error, element) {
                    placeError(error, element);
                },
                submitHandler: function(form) {
                    var cui = $('#contenttabs .ui-tabs-panel:visible'); 
               	    var jqxhr = $.post(
                        "<?=$action?>", 
                        $("#frmFilter<?=$frmtime?>").serialize(), 
                        function (data) {
                            $(cui).html(data);
                        }
                    );
                    $(cui).html('<div id="loadingdiv"><div class="centerdiv"><strong>Processing</strong><br/><img src="/images/ajaxloader.gif" /></div></div>');
                }
            });
            
            $("#frmFilter<?=$frmtime?> input[type='reset']").click( function() {
                $('#frmFilter<?=$frmtime?> label.error').remove();
            });
            
            $("#cda<?=$frmtime?>").click(function(){
                var objA = $(this);
                if(typeof objA.data('status') == 'undefined' || objA.data("status") == 'hidden') {
                    $("#cds<?=$frmtime?>").show(300);
                    objA.data('status',"active");
                    objA.html("Hide");
                } else if(objA.data("status") == 'active') {
                    $("#cds<?=$frmtime?>").hide(300);
                    objA.data('status',"hidden");
                    objA.html("Show");
                }
            });
            
            <?
            if($col_selected) {
            ?>
                $("#cda<?=$frmtime?>").trigger('click');
            <?
            }
            ?>
        });
    </script>
</div>